package com.bucg.cjcloud.gb28181.sip.handler;

import com.bucg.cjcloud.gb28181.sip.xmlbean.KeepaliveResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.sip.address.SipURI;
import javax.sip.header.FromHeader;
import javax.sip.message.Request;

/**
 * KeepaliveHandlerImpl..
 *
 * @author Lizhong
 * @date 2020/3/9
 */
@Component
public class KeepaliveHandlerImpl implements KeepaliveHandler {
    @Autowired
    RegisterHandler registerHandler;

    @Override
    public void handler(KeepaliveResp keepalive, Request request) {
        // 有时因注册周期时间未到,数据库无信息保存
        FromHeader from = (FromHeader) request.getHeader(FromHeader.NAME);
        SipURI sipURI_from = (SipURI) from.getAddress().getURI();
        String user = sipURI_from.getUser();
        boolean contains = ((RegisterHandlerImpl) registerHandler).getIsRegisted().contains(user);
        if (contains) return;

        registerHandler.handler(request, user);
    }
}
